let interval = null as any
/**
* @function     element table行滚动
* @description  将指定表格滚动到指定行
* @param        Ref 传入表格的ref实例
* @param        index 需要滚动到的行索引
* @return       void
*/
const tableScroll = (Ref:any, index:number) => {
    const bodyWrapper = Ref.value.$el.getElementsByClassName('el-table__body-wrapper')[0]
    if (bodyWrapper.scrollTo) {
        Ref.value.$el.getElementsByClassName('el-table__body-wrapper')[0].scrollTo({ top: 38 * index, behavior: 'smooth' })
    } else {
        clearInterval(interval)
        const maxTop = bodyWrapper.scrollHeight - bodyWrapper.clientHeight // 最大可滚动高度
        const targetTop = 38 * index < maxTop ? 38 * index : maxTop // 不可超过最大可滚动高度
        const step = targetTop - bodyWrapper.scrollTop > 0 ? 2 : -2 // 设置单帧滚动距离
        let timeStep = parseInt((500 / ((Math.abs(targetTop - bodyWrapper.scrollTop)) / 2)).toString(), 10) // 设置单位时间内的滚动时间步数
        if (timeStep > 10) timeStep = 10 // 时间步数不可超过10，要不然动画会很慢
        let lastTop = bodyWrapper.scrollTop // 上一次的top值
        interval = setInterval(() => {
            bodyWrapper.scrollTop += step
            if (lastTop === bodyWrapper.scrollTop) clearInterval(interval)
            else lastTop = bodyWrapper.scrollTop
            if (step > 0 && bodyWrapper.scrollTop >= targetTop) clearInterval(interval)
            else if (step < 0 && bodyWrapper.scrollTop <= targetTop) clearInterval(interval)
        }, timeStep)
    }
}

export {
    tableScroll,
}
